package com.guodi.model.utils;

import com.guodi.model.entity.IdtTargetValue;
import com.guodi.model.entity.IdtWarnRule;
import com.guodi.model.entity.IdtYearRelevance;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author tanght
 * @ClassName: JudgeWarnRule
 * @Description: 预警、达标规则工具类
 * @Date: 2020/12/5 16:48
 * @Version: 1.0
 */
@Component
public class JudgeWarnRuleUtil {

    /**
     * 预警状态判断
     * @param yearRelevanceList 指标对表信息列表
     * @param targetValueList 规划目标值列表
     * @param warnRule 预警规则
     * @param value 填报数值
     * @param monitorYear 填报年份
     * @param divisionCode 填报区域
     * @param idtId 指标id
     * @return
     */
    public static String getWarnName(List<IdtYearRelevance> yearRelevanceList, List<IdtTargetValue> targetValueList, IdtWarnRule warnRule, String value, Integer monitorYear, String divisionCode, String idtId) {
        if (yearRelevanceList == null || yearRelevanceList.size() == 0 || targetValueList == null && targetValueList.size() == 0) {
            return "";
        }

        Integer targetYear = null;
        // 通过填报年份获取对应的规划年份
        for (IdtYearRelevance yearRelevance : yearRelevanceList) {
            String monitorReportYear = yearRelevance.getMonitorReportYear();
            String[] split = monitorReportYear.split("-");
            if(split!=null && split.length==2) {
                Integer starYear = Integer.valueOf(split[0]);
                Integer endYear = Integer.valueOf(split[1]);
                if (idtId.equals(yearRelevance.getIndicatorsId()) && monitorYear >= starYear && monitorYear <= endYear) {
                    targetYear = yearRelevance.getPlanTargetYear();
                    continue;
                }
            }
        }

        String targetVal = "";
        for (IdtTargetValue targetValue : targetValueList) {
            if (targetValue.getIndicatorsId().equals(idtId) && targetValue.getDivisionCode().equals(divisionCode) && targetValue.getTargetYear().equals(targetYear)) {
                targetVal = targetValue.getTargetValue();
                continue;
            }
        }

        // 判断预警状态
        String idtWarnRules = null;
        if(null != warnRule){
            idtWarnRules =  warnRule.getName();
        }
        String warnStatu = null;
        //当预警规则为空或目标值为空或监测值为空时，预警状态未空值
        if(StringUtils.isBlank(targetVal) || StringUtils.isBlank(value)){
            warnStatu = null;
        } else {
            if ("正向".equals(idtWarnRules)) {
                if (Float.parseFloat(value) > Float.parseFloat(targetVal)) {
                    warnStatu = "正常";
                } else {
                    warnStatu = "预警";
                }
            } else if ("反向".equals(idtWarnRules)) {
                if (Float.parseFloat(value) < Float.parseFloat(targetVal)) {
                    warnStatu = "正常";
                } else {
                    warnStatu = "预警";
                }
            }
        }

        return warnStatu;

    }

    /**
     * 达标判断
     * @param yearRelevanceList 指标对表信息列表
     * @param targetValueList 规划目标值列表
     * @param warnRule 预警规则
     * @param value 填报数值
     * @param monitorYear 填报年份
     * @param divisionCode 填报区域
     * @param idtId 指标id
     * @return
     */
    public static Integer getIsStandard(List<IdtYearRelevance> yearRelevanceList, List<IdtTargetValue> targetValueList, IdtWarnRule warnRule, String value, Integer monitorYear, String divisionCode, String idtId) {
        if (yearRelevanceList == null || yearRelevanceList.size() == 0 || targetValueList == null && targetValueList.size() == 0) {
            return null;
        }

        Integer targetYear = null;
        // 通过填报年份获取对应的规划年份
        for (IdtYearRelevance yearRelevance : yearRelevanceList) {
            String monitorReportYear = yearRelevance.getMonitorReportYear();
            String[] split = monitorReportYear.split("-");
            if(split!=null && split.length==2) {
                Integer starYear = Integer.valueOf(split[0]);
                Integer endYear = Integer.valueOf(split[1]);
                if (idtId.equals(yearRelevance.getIndicatorsId()) && monitorYear >= starYear && monitorYear <= endYear) {
                    targetYear = yearRelevance.getPlanTargetYear();
                    continue;
                }
            }
        }

        String targetVal = "";
        for (IdtTargetValue targetValue : targetValueList) {
            if (targetValue.getIndicatorsId().equals(idtId) && targetValue.getDivisionCode().equals(divisionCode) && targetValue.getTargetYear().equals(targetYear)) {
                targetVal = targetValue.getTargetValue();
                continue;
            }
        }

        // 判断达标状态
        String idtWarnRules = null;
        if(null != warnRule){
            idtWarnRules =  warnRule.getName();
        }

        //未达标
        Integer isStandard = 0;

        if (StringUtils.isBlank(idtWarnRules) || StringUtils.isBlank(targetVal) || StringUtils.isBlank(value)) {
            isStandard = null;
        } else {
            if( "反向".equals(idtWarnRules)){
                if(Float.parseFloat(value) <= Float.parseFloat(targetVal)){
                    isStandard =1;
                }
            } else if( "正向".equals(idtWarnRules)) {
                if( Float.parseFloat(value) >= Float.parseFloat(targetVal)) {
                    isStandard = 1;
                }
            }
        }

        return isStandard;

    }
}
